package com.gljx.web.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Student;
import com.gljx.web.Entity.TrainCharge;
import com.gljx.web.Entity.TrainChargeEntry;
import com.gljx.web.dao.CustomLogMapper;
import com.gljx.web.dao.StudentMapper;
import com.gljx.web.dao.TrainChargeEntryMapper;
import com.gljx.web.dao.TrainChargeMapper;
import com.gljx.web.service.WeixinZxbmService;

@Service
@Transactional
public class WeixinZxbmServiceImpl implements WeixinZxbmService{

	@Autowired
    private StudentMapper studentDao;
	
	@Autowired
	private TrainChargeMapper trainDao;
	
	@Autowired
	private TrainChargeEntryMapper trainEntryDao;
	
	public void setPxsf(Student student,String xq,BigDecimal sf,String paNumber){
		
		//实付
		student.setCfreceived(sf);
		
		//公司ID
		String companyID = student.getCfficompanyid();
		
		//当前时间
		String nowTime = TimeFormat.timeFormat(new Date());
		
		//----------------------------插入培训单据母表
		TrainCharge train = new TrainCharge();
		
		//父单据fnumber
		train.setFnumber(paNumber);
		
		//父单据fid
		String paFid = GetFid.UUIDIntoFid();
		train.setFid(paFid);
		
		//时间
		train.setFcreatetime(nowTime);//创建时间
		train.setFbizdate(TimeFormat.getNowTime());//业务时间
		
		//公司
		train.setFcontrolunitid(companyID);
		train.setCfficompanyid(companyID);
		
		//单据状态 默认 4
		train.setCfbillstate(4L);
		trainDao.insertSelective(train);
		//---------------------------插入培训单据子表
		
		TrainChargeEntry trainEntry = new TrainChargeEntry();
		
		//子单据fid
		String fid = GetFid.UUIDIntoFid();
		trainEntry.setFid(fid);
		
		//默认序列 1
		trainEntry.setFseq(1L);
		
		//父单据
		trainEntry.setFparentid(paFid);
		//学员信息
		trainEntry.setStudentName(student.getFnameL2()+"");//学员名称
		trainEntry.setCfstudentid(student.getFid());		
		trainEntry.setCfstudentno(student.getFnumber());
		trainEntry.setCfidcardno(student.getCfidcardno());//身份证
		trainEntry.setCftjr(student.getCftjrName());//推荐人
		trainEntry.setCfclazz(student.getCfclazzName());//班级
		trainEntry.setCfysje(student.getCfamount());//应付
		trainEntry.setCfshje(sf);//实付
		
		//计算欠费
		BigDecimal yf = student.getCfamount();
		BigDecimal qf = yf.subtract(sf);
		trainEntry.setCfqfje(qf);
		//银行转账和已收、班级金额
		trainEntry.setCfyhzz(sf);
		trainEntry.setCfyishou(sf);
		trainEntry.setCfclazzamount(yf);
		trainEntryDao.insertSelective(trainEntry);
		Student updateStudent = new Student();
		//设置学员的已收和欠费信息
		updateStudent.setCfarrearage(qf);//欠费
		updateStudent.setCfreceived(sf);//已收
		//修改时间
		updateStudent.setFlastupdatetime(nowTime);
		updateStudent.setFid(student.getFid());
		studentDao.updateStudent(updateStudent);
		
	}
}
